package com.gudaomai.ekproject.service.impl;

import com.gudaomai.ekproject.entity.Permission;
import com.gudaomai.ekproject.entity.RolesPermissions;
import com.gudaomai.ekproject.mapper.PermissionMapper;
import com.gudaomai.ekproject.service.IPermissionService;
import com.gudaomai.ekproject.service.IRoleService;
import com.gudaomai.ekproject.service.IRolesPermissionsService;
import com.gudaomai.ekproject.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author zenghualu
 * @since 2017-06-26
 */
@Service
public class PermissionServiceImpl extends BaseServiceImpl<PermissionMapper, Permission> implements IPermissionService {

	@Autowired
	IRolesPermissionsService rolesPersionsServ;
	@Autowired
	IRoleService roleServ;

	/**
	 * 根据角色id获取所有权限列表
	 * 
	 * @author xiaosong
	 */
	@Override
	public List<Permission> getPermissionsByRoleId(String roleId) {

		List<Permission> permissions = null;
		// 当roleId不合法时，拒绝执行下面语句
		if (null != roleId && !"".equals(roleId) && roleServ.selectById(roleId) != null) {
			// 在角色权限表中根据role_id获取所有角色权限记录
			List<RolesPermissions> rolesPermissions = this.baseMapper.getRolesPermissionsByRoleId(roleId);
			// 从获取所有的记录将permission_id获取到一个集合中
			List<String> ids = new ArrayList<>();
			for (RolesPermissions rolesPermission : rolesPermissions) {
				ids.add(rolesPermission.getPermissionId());
			}

			// 通过批id方法获取用户权限
			// 对应的SQL语句为：select * from permission where permission_id in
			// (ids[0],ids[1],...)
			permissions = this.selectBatchIds(ids);

		}
		return permissions;
	}// method getPermissionByRoleId

}
